package cn.dyp.com.logprint.socket;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.Date;

import org.json.JSONException;
import org.json.JSONObject;

import android.content.Context;
import android.util.Base64;
import android.util.Log;

import cn.dyp.com.logprint.ServerApp;
import cn.dyp.com.logprint.bean.Sensor;
import cn.dyp.com.logprint.serial.SerialportService;

public class NotificationServerThread extends Thread {
	public static final String PM25_HIGH = "pm2.5 high";
	public static final String CO2_HIGH = "co2 high";
	public static final String CO2_LOW = "co2 low";
	public static final String LIGHT_HIGH = "light high";
	public static final String LIGHT_LOW = "light low";

	public static final String AIR_TMPER_HIGH = "air tmper high";
	public static final String AIR_TMPER_LOW = "air tmper low";
	public static final String AIR_HUMID_HIGH = "air humid high";
	public static final String AIR_HUMID_LOW = "air humid low";

	public static final String SOIL_TMPER_HIGH = "soil tmper high";
	public static final String SOIL_TMPER_LOW = "soil tmper low";
	public static final String SOIL_HUMID_HIGH = "soil humid high";
	public static final String SOIL_HUMID_LOW = "soil humid low";

	public enum SensorStatus {
		normal, low, high,
	}

	private static final String TAG = "NotificationServerThread";
	private Context mContext;
	private ServerSocket mSever;// 服务器socket
	private boolean mLogEnable = true; // 日志开关
	private volatile boolean mIsRun = true; // 线程控制开关

	private final static byte[] _singleLock = new byte[0];
	private long mNotifiId = 0;
	private SensorStatus mPm25Status = SensorStatus.normal;
	private SensorStatus mCo2Status = SensorStatus.normal;
	private SensorStatus mLightStatus = SensorStatus.normal;
	private SensorStatus mAirTStatus = SensorStatus.normal;
	private SensorStatus mAirHStatus = SensorStatus.normal;
	private SensorStatus mSoilTStatus = SensorStatus.normal;
	private SensorStatus mSoilHStatus = SensorStatus.normal;

	public NotificationServerThread(Context context, int port, boolean logEnable) {
		mContext = context;
		try {
			mSever = new ServerSocket(port); // 创建服务器socket
			mLogEnable = logEnable;
			mIsRun = true;
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	@Override
	public void run() {
		super.run();
		while (mIsRun) {
			try {
				// 当有客户端连接上时，创建客户端
				final Socket socket = mSever.accept();
				new Thread(new Runnable() {
					public void run() {
						if (mLogEnable) {
							Log.d(TAG, "notification socket accept:"
									+ socket.getInetAddress().toString());
						}
						BufferedReader in;
						try {
							in = new BufferedReader(new InputStreamReader(
									socket.getInputStream(), "UTF-8"));
							PrintWriter out = new PrintWriter(socket
									.getOutputStream());

							while (!socket.isClosed()) {
								// 读取客户端发送的内容
								String receiveStr;
								receiveStr = in.readLine();
								if (receiveStr == null)
									break;
								if (receiveStr != null
										&& !receiveStr.equals("")) {
									if (receiveStr.equals("getNotification")) {
										String socketResp = "";
										String notifiMsg = "";

										// if(isCanStartNoitifiServer()){
										checkAllSensorStatus();
										notifiMsg = getNotificationMsg();
										// }

										JSONObject jsonResponse = new JSONObject();
										try {
											jsonResponse.put("notifiId",
													mNotifiId);
											jsonResponse.put("notifiMsg",
													notifiMsg);
											socketResp = jsonResponse
													.toString();
											Log.d("test1", socketResp);
										} catch (JSONException e) {
											e.printStackTrace();
										}
										out.println(Base64.encodeToString(
												socketResp.getBytes(),
												Base64.DEFAULT));
									}
									out.flush();
								}
							}
							// 主动关闭socket连接
							socket.close();
							if (mLogEnable) {
								Log.d(TAG, "notification socket close:"
										+ socket.getInetAddress().toString());
							}
						} catch (IOException e) {
							e.printStackTrace();
						}
					}
				}).start();// 开始启动线程

			} catch (IOException e) {
				e.printStackTrace();
			}
		}
	}

	private void checkAllSensorStatus() {
		synchronized (_singleLock) {
			boolean isHasChange = false;

			// 获取所有传感器当前的值
			int pm25Value = SerialportService
					.getSensorValueByName(Sensor.PM25_SENSOR);// PM2.5的值
			int co2Value = SerialportService
					.getSensorValueByName(Sensor.CO2_SENSOR);// co2浓度
			int lightValue = SerialportService
					.getSensorValueByName(Sensor.LIGHT_SENSOR);// 光照强度
			int airTValue = SerialportService
					.getSensorValueByName(Sensor.AIR_TMPER_SENSOR);// 空气温度
			int airHValue = SerialportService
					.getSensorValueByName(Sensor.AIR_HUMID_SENSOR);// 空气湿度
			int soilTValue = SerialportService
					.getSensorValueByName(Sensor.SOIL_TMPER_SENSOR);// 土壤温度
			int soilHValue = SerialportService
					.getSensorValueByName(Sensor.SOIL_HUMID_SENSOR);// 土壤湿度

			ServerApp app = (ServerApp) mContext;
			// 存储所有传感器的状态
			SensorStatus pm25Status = SensorStatus.normal;
			SensorStatus co2Status = SensorStatus.normal;
			SensorStatus lightStatus = SensorStatus.normal;
			SensorStatus airTStatus = SensorStatus.normal;
			SensorStatus airHStatus = SensorStatus.normal;
			SensorStatus soilTStatus = SensorStatus.normal;
			SensorStatus soilHStatus = SensorStatus.normal;

			// 通过比较，得出所有传感器的状态值
			if (pm25Value > app.getMaxPm25()) {
				pm25Status = SensorStatus.high;
			}

			if (co2Value > app.getMaxCo2()) {
				co2Status = SensorStatus.high;
			}
			if (co2Value < app.getMinCo2()) {
				co2Status = SensorStatus.low;
			}
			if (lightValue > app.getMaxLight()) {
				lightStatus = SensorStatus.high;
			}
			if (lightValue < app.getMinLight()) {
				lightStatus = SensorStatus.low;
			}

			if (airTValue > app.getMaxAirTemperature()) {
				airTStatus = SensorStatus.high;
			}
			if (airTValue < app.getMinAirTemperature()) {
				airTStatus = SensorStatus.low;
			}
			if (airHValue > app.getMaxAirHumidity()) {
				airHStatus = SensorStatus.high;
			}
			if (airHValue < app.getMinAirHumidity()) {
				airHStatus = SensorStatus.low;
			}

			if (soilTValue > app.getMaxEarthTemperature()) {
				soilTStatus = SensorStatus.high;
			}
			if (soilTValue < app.getMinEarthTemperature()) {
				soilTStatus = SensorStatus.low;
			}
			if (soilHValue > app.getMaxEarthHumidity()) {
				soilHStatus = SensorStatus.high;
			}
			if (soilHValue < app.getMinEarthHumidity()) {
				soilHStatus = SensorStatus.low;
			}

			// 将新得到状态值赋给对象
			SensorStatus newValues[] = { pm25Status, co2Status, lightStatus,
					airTStatus, airHStatus, soilTStatus, soilHStatus };
			SensorStatus preValues[] = { mPm25Status, mCo2Status, mLightStatus,
					mAirTStatus, mAirHStatus, mSoilTStatus, mSoilHStatus };
			for (int i = 0; i < preValues.length; i++) {
				if (newValues[i] != preValues[i]) {
					isHasChange = true;
				}
			}

			mPm25Status = pm25Status;
			mCo2Status = co2Status;
			mLightStatus = lightStatus;
			mAirTStatus = airTStatus;
			mAirHStatus = airHStatus;
			mSoilTStatus = soilTStatus;
			mSoilHStatus = soilHStatus;

			if (isHasChange) {
				mNotifiId++;
			}
			Log.d("test1 id", mNotifiId + "");
		}
	}

	private String getNotificationMsg() {
		String notifiMsg = "";
		// pm2.5偏高
		if (mPm25Status == SensorStatus.high) {
			notifiMsg += PM25_HIGH + ",";
		}
		// Co2偏高
		if (mCo2Status == SensorStatus.high) {
			notifiMsg += CO2_HIGH + ",";
		}
		if (mCo2Status == SensorStatus.low) {
			notifiMsg += CO2_LOW + ",";
		}
		if (mLightStatus == SensorStatus.high) {
			notifiMsg += LIGHT_HIGH + ",";
		}
		if (mLightStatus == SensorStatus.low) {
			notifiMsg += LIGHT_LOW + ",";
		}
		if (mAirTStatus == SensorStatus.high) {
			notifiMsg += AIR_TMPER_HIGH + ",";
		}
		if (mAirTStatus == SensorStatus.low) {
			notifiMsg += AIR_TMPER_LOW + ",";
		}
		if (mAirHStatus == SensorStatus.high) {
			notifiMsg += AIR_HUMID_HIGH + ",";
		}
		if (mAirHStatus == SensorStatus.low) {
			notifiMsg += AIR_HUMID_LOW + ",";
		}
		if (mSoilTStatus == SensorStatus.high) {
			notifiMsg += SOIL_TMPER_HIGH + ",";
		}
		if (mSoilTStatus == SensorStatus.low) {
			notifiMsg += SOIL_TMPER_LOW + ",";
		}
		if (mSoilHStatus == SensorStatus.high) {
			notifiMsg += SOIL_HUMID_HIGH + ",";
		}
		if (mSoilHStatus == SensorStatus.low) {
			notifiMsg += SOIL_HUMID_LOW + ",";
		}
		return notifiMsg;
	}

	// 通知控制逻辑要在服务开启10秒以后再启动
	private boolean isCanStartNoitifiServer() {
		Date now = new Date();
		if (SerialportService.mStartTime != null
				&& now.getTime() - SerialportService.mStartTime.getTime() > 10000) {
			return true;
		} else {
			return false;
		}
	}

	// 停止服务器端server
	public void stopServer() {
		mIsRun = false;
		try {
			mSever.close();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

}
